/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package etherblocks.engine.network.handlers;

/**
 *
 * @author Carl
 */
public class BlockChunkMessageHandler{

    public BlockChunkMessageHandler(){
        
    }
    public static final int MESSAGES_PER_CHUNK = 4;
    private int receivedMessages;
    private byte[] terrainData = new byte[0];
    
    public void onChunkPartReceived(byte[] partData){
        if(isComplete()){
            terrainData = new byte[0];
        }
        byte[] newTerrainData = new byte[terrainData.length + partData.length];
        for(int i=0;i<terrainData.length;i++){
            newTerrainData[i] = terrainData[i];
        }
        for(int i=0;i<partData.length;i++){
            newTerrainData[terrainData.length + i] = partData[i];
        }
        terrainData = newTerrainData;
        receivedMessages++;
    }
    
    public boolean isComplete(){
        return (receivedMessages >= MESSAGES_PER_CHUNK);
    }

    public byte[] getTerrainData(){
        return terrainData;
    }
}
